import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1547529799954&di=a226cddd0b2c15e7afe269ac7e952817&imgtype=0&src=http%3A%2F%2Fimg1.meichubang.com%2Fpic%2F201501%2F10%2Fd2cba07b6873808f3ff86be4bcca69bc.jpg'
export default new Vuex.Store({
  state: {
    goodsList:[
      {
        id:20001,
        name:'法式布蕾蛋糕 Pandora',
        data:[
          {
            id:20011,
            size:'4寸 12cmx12cm',
            price:'￥120.00',
            tip:['含2件餐具','适合1-2人分享'],
            count:99
          },
          {
            id:20021,
            size:'5寸 14cmx14cm',
            price:'￥170.00',
            tip:['含4件餐具','适合3-4人分享'],
            count:30,
          },
          {
            id:20031,
            size:'6寸 16cmx16cm',
            price:'￥198.00',
            tip:['含6件餐具','适合4-6人分享'],
            count:2
          },
        ],
        url,
      }    
    ],
    cartList: [
      {
        id: 10001,
        name: "法式布蕾蛋糕 Pandora",
        size1: "尺寸：6寸",
        size2: "含五盘五叉",
        price: "￥198.50",
        count: 1,
        checked: false,
        url
      },
      {
        id: 10002,
        name: "法式布蕾蛋糕 Pandora",
        size1: "尺寸：6寸",
        size2: "含五盘五叉",
        price: "￥198.45",
        count: 2,
        checked: true,
        url
      },
      {
        id: 10003,
        name: "法式布蕾蛋糕 Pandora",
        size1: "尺寸：6寸",
        size2: "含五盘五叉",
        price: "￥198.00",
        count: 3,
        checked: false,
        url
      }
    ],
  },
  mutations: {
    updateCartList({cartList},{index,data,type}){
      switch (type) {
        case 0:
          cartList.push(data)
          break;
        case 1:
          cartList.splice(index,1)
          break;
        case 2:
          cartList.length = 0
          break;
        default:
          break;
      }
    },
    updateCount({cartList},{index,count}){
      cartList[index].count = count
    }
  },
  actions: {
    //更新购物车列表 添加 删除与 删除全部
    updateCartList({commit,state},{data,type}){
      let index
      if(data.id){
         index = state.cartList.findIndex((item)=> item.id === data.id)
      }
      switch (type) {
        // 添加 如果列表有已存在 则更新数量
        case 0:
              if(index !== -1){
                let count = state.cartList[index].count
                count++
                 commit('updateCount',{index,count})
              }else{
                commit('updateCartList',{data,type})
              }
          break;
          // 删除
          case 1:
              commit('updateCartList',{index,type})
          break;
          // 删除全部
          case 2:
              commit('updateCartList',{type})
          break;
        default:
          break;
      }
    },
    //更新购物车列表中 某一个的 数量
    updateCount({commit,state},{id,count}){
      let index =  state.cartList.findIndex((item) => item.id === id)
      commit('updateCount',{index,count})
    }
  },
  getters:{
    //统计购物车数量
    count({cartList}){
      let count = 0
      cartList.forEach(item => {
        count += item.count
      });
      return count
    }
  }
})
